package com.shujia.java.day17.demo1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
* @Description: Demo4 测试读写效率
* @Author: 王火火
* @Date: 2019/8/19 9:38
 *  第一种字节流
 *          一个一个  耗时:29762毫秒
 *          一个数组  耗时:47毫秒
 *  第二种缓冲流
 *          一个一个 耗时:172毫秒
 *          一个数组 耗时:16毫秒
 *
*/ 
public class Demo4 {
    public static void main(String[] args) throws Exception{
        long start = System.currentTimeMillis();
        //showTime1("F:\\data\\Linux.docx","F:\\data\\LinuxCopy.docx");
        //showTime2("F:\\data\\Linux.docx","F:\\data\\LinuxCopy1.docx");
        //showTime3("F:\\data\\Linux.docx","F:\\data\\LinuxCopy2.docx");
        showTime4("F:\\data\\Linux.docx","F:\\data\\LinuxCopy3.docx");

        long end = System.currentTimeMillis();
        System.out.println("耗时:"+(end-start)+"毫秒");
    }
    //字节流 一个一个读取
    public static void showTime1(String startPath,String endPath)throws Exception{
        //创建流
        FileInputStream fis = new FileInputStream(startPath);
        FileOutputStream fos = new FileOutputStream(endPath);

        int i;
        while ((i=fis.read())!=-1){
            fos.write(i);
        }
        //关闭流
        fos.close();
        fis.close();
    }
    //字节流 一个数组读取
    public static void showTime2(String startPath,String endPath)throws Exception{
        //创建流
        FileInputStream fis = new FileInputStream(startPath);
        FileOutputStream fos = new FileOutputStream(endPath);
        byte[] bytes = new byte[1024];
        int len;
        while ((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        //关闭流
        fos.close();
        fis.close();

    }
    //缓冲流 一个一个读取
    public static void showTime3(String startPath,String endPath)throws Exception{

        //创建流
        FileInputStream fis = new FileInputStream(startPath);
        BufferedInputStream bis = new BufferedInputStream(fis);


        FileOutputStream fos = new FileOutputStream(endPath);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        int len;
        while ((len=bis.read())!=-1){
            bos.write(len);
        }
        //关闭流
        bos.close();
        bis.close();
        

    }
    //缓冲流 一个数组读取
    public static void showTime4(String startPath,String endPath)throws Exception{

        //创建流
        FileInputStream fis = new FileInputStream(startPath);
        BufferedInputStream bis = new BufferedInputStream(fis);


        FileOutputStream fos = new FileOutputStream(endPath);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        byte[] bytes = new byte[1024];
        int len;
        while ((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        //关闭流
        bos.close();
        bis.close();

    }
}
